import httpUtil from "@/utils/httpUtil.js"
import userApi from "./userApi"


const initData = (option) => {
	let params = {
			v1:1,
			v2:101,
			text:JSON.stringify(option)
		}
		httpUtil.post('/api/rf/addLog', params, res => {
			
		})
	if (option.a) {
		httpUtil.setA(option.a)
	}
	if (option.openid) {
		userApi.setUserId(option.openid)
	}
	if (option.qcjParamStr) {
		setParam(option.qcjParamStr)
	}
	if (option.param) {
		setParam(option.param)
	}
	if (option.jtcAdRequestId) {
		setParam(option.jtcAdRequestId)
	}
	if (option.push_id) {
		setParam(option.push_id)
		setPushId(option.push_id)
	}
	if (option.pushid) {
		setParam(option.pushid)
		setPushId(option.pushid)
	}
	if (option.token) {
		setParamToken(option.token)
	}

	if (option.clickid) {
		setParam(option.clickid)
	}
	if (option.tokenid) {
		setParam(option.tokenid)
	}
	if (option.tokenId) {
		setParam(option.tokenId)
	}
	if (option.union_site) {
		setParam(window.location.href)
	}
	
	if (option.adId) {
		setParam(option.adId)
	}
	if(option.requestId){
		console.log(option.requestId)
		let a={
			'requestId':option.requestId,
			'adId':option.adId,
			'pn':option.pn
		}
		console.log(a)
		if(a.requestId==null||a.requestId==undefined||a.requestId=='null'){
			a.requestId=''
		}
		if(a.adId==null||a.adId==undefined||a.adId=='null'){
			a.adId=''
		}
		if(a.pn==null||a.pn==undefined||a.pn=='null'){
			a.pn=''
		}
		console.log(JSON.stringify(a))
		setParam(JSON.stringify(a))
		
	}
	if(option.projectid){
	
		// let a={
		// 	'projectid':option.projectid,
		// 	'promotionid':option.promotionid,
		// 	'creativetype':option.creativetype,
		// 	'clickid':option.clickid
		// }
		// console.log(a)
		// if(a.projectid==null||a.projectid==undefined||a.projectid=='null'){
		// 	a.projectid=''
		// }
		// if(a.promotionid==null||a.promotionid==undefined||a.promotionid=='null'){
		// 	a.promotionid=''
		// }
		// if(a.creativetype==null||a.creativetype==undefined||a.creativetype=='null'){
		// 	a.creativetype=''
		// }
		// if(a.clickid==null||a.clickid==undefined||a.clickid=='null'){
		// 	a.clickid=''
		// }
		// console.log(JSON.stringify(a))
		setParam(window.location.href)
		
	}
	if(option.creativeid){
	
	setParam(window.location.href)
		
	}
	
}

const setParam = (value) => {
	uni.setStorageSync('param', value)
}

const getParam = () => {
	console.log('getParam1', getPushId(), getParamToken())
	if (getPushId() && getParamToken()) {
		console.log('getParam2')
		return JSON.stringify({
			"push_id": getPushId(),
			"token": getParamToken()
		})
	}
	console.log('getParam3')
	return uni.getStorageSync('param')
}

const removeParam = () => {
	uni.removeStorageSync('param')
}

const setPushId = (value) => {
	uni.setStorageSync('paramPushId', value)
}

const getPushId = () => {
	return uni.getStorageSync('paramPushId')
}

const removePushId = () => {
	uni.removeStorageSync('paramPushId')
}

const setParamToken = (value) => {
	uni.setStorageSync('paramToken', value)
}

const getParamToken = () => {
	return uni.getStorageSync('paramToken')
}

const removeParamToken = () => {
	uni.removeStorageSync('paramToken')
}

const clearParam = () => {
	removeParam()
	removeParamToken()
	removePushId()
}

export default {
	initData,
	setParam,
	getParam,
	setParamToken,
	getParamToken,
	setPushId,
	getPushId,
	clearParam,
}